package controllers;

import java.util.*;

import models.*;
import play.mvc.Controller;

public class Requests extends Controller {
	/**
	 * renders the entity to the request Topic view that has a form that takes
	 * input of the details of the topic request.
	 * 
	 * @param entityId
	 *            The entity id is used to retrieve the entity to which the
	 *            request will be sent to its organizers. the id will be
	 *            rendered to the page.
	 * 
	 * @author Shahir El Faramawy
	 */
	public static void requestTopic(Long entityId) {
		Entities entity = Entities.findById(entityId);
		render(entity);
	}

	/**
	 * retrieves the information from the parameters that were forwarded from
	 * the form and creates the topic request.
	 * 
	 * @param entityId
	 *            will be passed as a parameter to the method sendTopicRequest.
	 * @author Shahir El Faramawy
	 */
	public static void topicRequestSent(Long entityId) {
		try {
			String idString = session.get("userId");
			if (idString == null) {
				Home.home(null, null, "Please Login");
			}
			Long userId = Long.parseLong(idString);
			User user = User.findById(userId);
			String title = params.get("subject");
			String body = params.get("Body");
			user.sendTopicRequest(title, body, entityId);
			render(entityId);
		} catch (NullPointerException error) {
			Requests.contentNotFound();
		}
	}

	/**
	 * renders the list of topic requests in a specific entity
	 * 
	 * @param entityId
	 *            the entity id to retrieve the list or topic requets in that
	 *            entity
	 * 
	 * @author Shahir El Faramawy
	 */
	public static void viewTopicRequest(Long entityId) {
		try {
			List<Request> requestList = Request.find("byENTITYREQUEST_ID",
					entityId).fetch();
			render(requestList);

		} catch (NullPointerException error) {
			Requests.contentNotFound();
		}

	}

	/**
	 * renders the topic request to the view.
	 * 
	 * @param requestId
	 *            the id of the topic request that will be retrieved
	 * @author Shahir El Faramawy
	 */
	public static void acceptTopicRequest(Long requestId) {
		try {
			Request requestTopic = Request.findById(requestId);
			Long entityId = requestTopic.entityRequest.id;
			render(requestTopic, entityId);
		} catch (NullPointerException error) {
			Requests.contentNotFound();
		}

	}

	/**
	 * deletes the topic request once it is rejected.
	 * 
	 * @param requestId
	 *            the id of the topic request that will be retrieved
	 * @author Shahir El Faramawy
	 */

	public static void rejectTopicRequest(Long requestId, Long entityId) {
		try {
			Request requestTopic = Request.findById(requestId);
			requestTopic.delete();
			Long showEntityId = entityId;
			render(showEntityId);
		} catch (NullPointerException error) {
			Requests.contentNotFound();
		}

	}

	/**
	 * gets a page that has a header of no content found
	 * 
	 * @author Shahir El Faramawy
	 */
	public static void contentNotFound() {
		render();
	}

}
